// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <melon/container/f14_map.h>
#include <pollux/common/base/gtest_macros.h>
#include <pollux/dwio/dwrf/common/common.h>
#include <pollux/dwio/dwrf/common/encryption.h>
#include <pollux/dwio/dwrf/common/wrap/dwrf-proto-wrapper.h>
#include <pollux/dwio/dwrf/writer/writer_context.h>

namespace kumo::pollux::dwrf {
using StreamList =
    std::vector<std::pair<const DwrfStreamIdentifier*, DataBufferHolder*>>;

StreamList getStreamList(WriterContext& context);

class EncodingIter {
 public:
  using value_type = const proto::ColumnEncoding;
  using reference = const proto::ColumnEncoding&;
  using pointer = const proto::ColumnEncoding*;
  using iterator_category = std::forward_iterator_tag;
  using difference_type = int64_t;

  static EncodingIter begin(
      const proto::StripeFooter& footer,
      const std::vector<proto::StripeEncryptionGroup>& encryptionGroups);

  static EncodingIter end(
      const proto::StripeFooter& footer,
      const std::vector<proto::StripeEncryptionGroup>& encryptionGroups);

  EncodingIter& operator++();
  EncodingIter operator++(int);
  bool operator==(const EncodingIter& other) const;
  bool operator!=(const EncodingIter& other) const;
  reference operator*() const;
  pointer operator->() const;

 private:
  EncodingIter(
      const proto::StripeFooter& footer,
      const std::vector<proto::StripeEncryptionGroup>& encryptionGroups,
      int32_t encryptionGroupIndex,
      google::protobuf::RepeatedPtrField<proto::ColumnEncoding>::const_iterator
          current,
      google::protobuf::RepeatedPtrField<proto::ColumnEncoding>::const_iterator
          currentEnd);

  void next();

  POLLUX_FRIEND_TEST(TestEncodingIter, Ctor);
  POLLUX_FRIEND_TEST(TestEncodingIter, EncodingIterBeginAndEnd);
  bool emptyEncryptionGroups() const;

  const proto::StripeFooter& footer_;
  const std::vector<proto::StripeEncryptionGroup>& encryptionGroups_;
  int32_t encryptionGroupIndex_{-1};
  google::protobuf::RepeatedPtrField<proto::ColumnEncoding>::const_iterator
      current_;
  google::protobuf::RepeatedPtrField<proto::ColumnEncoding>::const_iterator
      currentEnd_;
};

class EncodingContainer {
 public:
  virtual ~EncodingContainer() = default;
  virtual EncodingIter begin() const = 0;
  virtual EncodingIter end() const = 0;
};

class EncodingManager : public EncodingContainer {
 public:
  explicit EncodingManager(
      const encryption::EncryptionHandler& encryptionHandler);
  virtual ~EncodingManager() override = default;

  proto::ColumnEncoding& addEncodingToFooter(uint32_t nodeId);
  proto::Stream* addStreamToFooter(uint32_t nodeId, uint32_t& currentIndex);
  std::string* addEncryptionGroupToFooter();
  proto::StripeEncryptionGroup getEncryptionGroup(uint32_t i);
  const proto::StripeFooter& getFooter() const;

  EncodingIter begin() const override;
  EncodingIter end() const override;

 private:
  void initEncryptionGroups();

  const encryption::EncryptionHandler& encryptionHandler_;
  proto::StripeFooter footer_;
  std::vector<proto::StripeEncryptionGroup> encryptionGroups_;
};

class LayoutResult {
 public:
  LayoutResult(StreamList streams, size_t indexCount)
      : streams_{std::move(streams)}, indexCount_{indexCount} {}

  void iterateIndexStreams(
      const std::function<void(const DwrfStreamIdentifier&, DataBufferHolder&)>&
          consumer) const;

  void iterateDataStreams(
      const std::function<void(const DwrfStreamIdentifier&, DataBufferHolder&)>&
          consumer) const;

 private:
  const StreamList streams_;
  const size_t indexCount_;
};

class LayoutPlanner {
 public:
  explicit LayoutPlanner(const dwio::common::TypeWithId& schema);
  virtual ~LayoutPlanner() = default;

  virtual LayoutResult plan(
      const EncodingContainer& encoding,
      StreamList streamList) const;

 protected:
  static void sortBySize(
      StreamList::iterator begin,
      StreamList::iterator end,
      const melon::F14FastSet<uint32_t>& flatMapCols);

  // This method assumes flatmap can only be top level fields, which is enforced
  // through the way how flatmap is configured.
  static melon::F14FastSet<uint32_t> getFlatMapColumns(
      const EncodingContainer& encoding,
      const melon::F14FastMap<uint32_t, uint32_t>& nodeToColumnMap);

  melon::F14FastMap<uint32_t, uint32_t> nodeToColumnMap_;

  POLLUX_FRIEND_TEST(LayoutPlannerTests, Basic);
};

} // namespace kumo::pollux::dwrf
